package com.pagepub.expo.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.stereotype.Repository;

import com.pagepub.expo.entity.AppUser;
import com.pagepub.expo.entity.CorpCatalog;
import com.pagepub.expo.entity.CorpPavilionCommpany;
import com.pagepub.expo.entity.IdName;
@Repository
public class CorpPavilionCommpanyDao {
	@Autowired
	@Qualifier("jdbcTemplate")
	private JdbcTemplate template;
	
	@Autowired
	@Qualifier("appSeqDao")
	private AppSeqDao appSeqDao;
	
	private static String tableName="Corp_Pavilion_Commpany";
	
	private static String QUERY_ALL = "SELECT id,corpId,pavilionId,xWidth,yHeight,zzoom,roomNumber  FROM "+tableName;

	private static String QUERY_ALL_COUNT = "SELECT COUNT(*) FROM "+  tableName;
	
	private static String INSERT_SQL = "INSERT  INTO "+tableName+" (" +
			"id,corpId,pavilionId,xWidth,yHeight,zzoom,roomNumber)VALUES(" +
			":id,:corpId,:pavilionId,:xWidth,:yHeight,:zzoom,:roomNumber)";
	

	private static String DELETE_SQL = "DELETE FROM "+ tableName +" WHERE id=?";

	private static String UPDATE_SQL = "UPDATE "+ tableName + " SET " +
			"corpId=:corpId,pavilionId=:pavilionId,xWidth=:xWidth,yHeight=:yHeight,zzoom=:zzoom,roomNumber=:roomNumber"+
			" WHERE id=:id";

	private final ParameterizedRowMapper<CorpPavilionCommpany> mapper = new ParameterizedRowMapper<CorpPavilionCommpany>() {
		public CorpPavilionCommpany mapRow(ResultSet rs, int rowNum) throws SQLException {
			CorpPavilionCommpany obj = new CorpPavilionCommpany();
			obj.setId(rs.getLong("id"));
			obj.setCorpId(rs.getString("corpId"));
			obj.setPavilionId(rs.getInt("pavilionId"));
			obj.setxWidth(rs.getShort("xWidth"));
			obj.setyHeight(rs.getShort("yHeight"));
			obj.setZzoom(rs.getShort("zzoom"));
			obj.setRoomNumber(rs.getInt("roomNumber"));
			return obj;
		}
	};

	public List<CorpPavilionCommpany> findAll() {
		return template.query(QUERY_ALL, mapper);
	}

	public CorpPavilionCommpany findById(Long id) {
		List<CorpPavilionCommpany> list = template.query(QUERY_ALL + " WHERE id=?",
				new Object[] { id }, mapper);
		if (list.size() == 0) {
			return null;
		} else
			return list.get(0);
	}
	
	

	
	public void insert(CorpPavilionCommpany bean) {
		if(bean.getId()==null){
			bean.setId(new Long(appSeqDao.getSequence("corp_catalog_seq")));
		}		
		SqlParameterSource ps = new BeanPropertySqlParameterSource(bean);
		NamedParameterJdbcTemplate npTemplate = new NamedParameterJdbcTemplate(
				template.getDataSource());
		npTemplate.update(INSERT_SQL, ps);

	}

	public void delete(Long id) {
		template.update(DELETE_SQL, id);
	}

	public void update(CorpPavilionCommpany bean) {
		SqlParameterSource ps = new BeanPropertySqlParameterSource(bean);
		NamedParameterJdbcTemplate npTemplate = new NamedParameterJdbcTemplate(
				template.getDataSource());
		npTemplate.update(UPDATE_SQL, ps);
	}
	
	public int getCount(Map<String, String> param){
		int count = template.queryForInt(QUERY_ALL_COUNT);
		return count;
	}
	
	public List findByPage(int currentPage,int pageSize,Map<String, String> param){
		List lists = template.query(QUERY_ALL + " limit ?,?",
				new Object[] { currentPage, pageSize}, mapper);
		return lists;
		
	}
	

}
